using System;
using System.Collections.Generic;

namespace Nyanimals
{
	public sealed class CStateMachine
	{
		private static readonly CStateMachine instance = new CStateMachine();
		private LinkedList<CBaseState> States;
		private bool noRender;
		
		private CStateMachine ()
		{
			States = new LinkedList<CBaseState>();
			noRender = true;
		}
		public static CStateMachine Instance
		{
			get { return instance;}
		}
		
		public void Input()
		{
			States.Last.Value.Input();
		}
		
		public void Update(float deltaTime)
		{
			States.Last.Value.Update(deltaTime);
			noRender = false;
		}
		
		public void Render()
		{
			if(noRender)
			{
				return;
			}
			States.Last.Value.Render();
		}
		
		public void PushState(CBaseState state)
		{
			state.Enter();
			States.AddLast(state);
			noRender = true;
		}
		
		public void PopState()
		{
			if(States.Count > 0)
			{
				States.Last.Value.Exit();
				States.RemoveLast();
			}
		}
		
		public void ChangeState(CBaseState state)
		{
			foreach(CBaseState s in States)
			{
				s.Exit();
			}
			States.Clear();
			PushState(state);
		}
		
		StateTypes GetStateType()
		{
			return States.Last.Value.GetStateType();
		}
	}
}

